package com.bmob.lover.ui.fragment.lol

import android.view.View
import androidx.fragment.app.viewModels
import com.qmuiteam.qmui.widget.dialog.QMUIBottomSheet
import com.bmob.lover.R
import com.bmob.lover.viewmodel.EventViewModel
import com.bmob.lover.viewmodel.request.LOLWelfareActionViewModel
import com.theone.mvvm.core.data.entity.QMUIItemBean
import com.theone.mvvm.core.ui.fragment.ImagePreviewFragment
import com.theone.mvvm.ext.getAppViewModel
import com.theone.mvvm.ext.qmui.showFailTipsDialog
import com.theone.mvvm.ext.qmui.showSuccessTipsDialog

//  ┏┓　　　┏┓
//┏┛┻━━━┛┻┓
//┃　　　　　　　┃
//┃　　　━　　　┃
//┃　┳┛　┗┳　┃
//┃　　　　　　　┃
//┃　　　┻　　　┃
//┃　　　　　　　┃
//┗━┓　　　┏━┛
//    ┃　　　┃                  神兽保佑
//    ┃　　　┃                  永无BUG！
//    ┃　　　┗━━━┓
//    ┃　　　　　　　┣┓
//    ┃　　　　　　　┏┛
//    ┗┓┓┏━┳┓┏┛
//      ┃┫┫　┃┫┫
//      ┗┻┛　┗┻┛
/**
 * @author The one
 * @date 2021-05-27 14:37
 * @describe TODO
 * @email 625805189@qq.com
 * @remark
 */
class LOLImagePreviewFragment : ImagePreviewFragment() {

    private val mActionVm: LOLWelfareActionViewModel by viewModels()
    private val mEventVm: EventViewModel by lazy { getAppViewModel<EventViewModel>() }

    private val ITEM_PUBLISH = "发布"

    override fun createObserver() {
        addLoadingObserve(mActionVm)
        super.createObserver()
        mActionVm.run {
            getObjectSuccessLiveDate().observe(this@LOLImagePreviewFragment) {
                mEventVm.onPublishSuccess(mTableName)
                showSuccessTipsDialog(it.msg)
            }
            getErrorLiveData().observe(this@LOLImagePreviewFragment) {
                showFailTipsDialog(
                    if (it.contains("unique index cannot has duplicate value")) {
                        "此图片已上传"
                    } else {
                        it
                    }
                )
            }
        }
    }

    override fun initGridBottomItems(items: MutableList<QMUIItemBean>) {
        super.initGridBottomItems(items)
        items.add(QMUIItemBean(ITEM_PUBLISH,R.drawable.svg_sheet_publish))
    }

    override fun onGridBottomSheetItemClick(dialog: QMUIBottomSheet, itemView: View, tag: QMUIItemBean) {
        super.onGridBottomSheetItemClick(dialog, itemView, tag)
        if(tag.getItemTitle() == ITEM_PUBLISH){
            mClickData?.let {
                mActionVm.save(it.getImageUrl())
            }
        }
    }

}